package com.example.starter.work6

import org.apache.rocketmq.client.producer.DefaultMQProducer
import org.apache.rocketmq.client.producer.SendResult
import org.apache.rocketmq.common.message.Message
import org.apache.rocketmq.remoting.common.RemotingHelper
import java.text.SimpleDateFormat
import java.time.Duration
import java.time.LocalDateTime
import java.util.*


/**
@author whd
@date 2023/10/12 14:23:46
 */

fun main() {
  val producer = DefaultMQProducer("test-producer", false)
  producer.namesrvAddr = "192.168.201.129:9876"
  producer.start()
  val currentTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
  for (i in 0..9) {
    val msg = Message()
    msg.topic = "delay-topic"
    msg.body = "这是一条延迟消息".toByteArray(charset(RemotingHelper.DEFAULT_CHARSET))

    val messageDelayTime: Duration = Duration.ofSeconds(76)
    val delayTimestamp: Long = System.currentTimeMillis() + messageDelayTime.toMillis()
    // 绝对时间：定时消息
    msg.deliverTimeMs = delayTimestamp
    // 相对时间：延时消息
           // msg.setDelayTimeSec(1000 * 5);

    val sendResult: SendResult = producer.send(msg)
    System.out.printf("发送时间：%s %n", currentTime)
  }
  producer.shutdown()
}

